package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sc.nft.dao.SupplierManageDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.SupplierManage;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.*;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.SupplierManageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 供应商管理(SupplierManage)表服务实现类
 *
 * @author 王伟成
 * @since 2023-10-17 15:29:38
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SupplierManageServiceImpl implements SupplierManageService {

    private final SupplierManageDao supplierManageDao;

    private final UserInfoDao userInfoDao;
    @Override
    public void save(SupplierManageSaveDTO supplierManageSaveDTO) {
        SupplierManage supplierManage = new SupplierManage();
        BeanUtil.copyProperties(supplierManageSaveDTO, supplierManage);
        supplierManageDao.save(supplierManage);
    }
    @Override
    public void edit(SupplierManageEditDTO supplierManageEditDTO) {
        SupplierManage supplierManage = new SupplierManage();
        BeanUtil.copyProperties(supplierManageEditDTO, supplierManage);
        supplierManage.setId(supplierManageEditDTO.getId());
        supplierManageDao.updateById(supplierManage);
    }
    @Override
    public IPage<SupplierManagePageVO> page(SupplierManagePageDTO supplierManagePageDTO) {
        return supplierManageDao.page(supplierManagePageDTO);
    }
    @Override
    public SupplierManagePageVO getById(Long id) {
        return supplierManageDao.getById(id);
    }
    @Override
    public void removeById(Long id) {
        supplierManageDao.removeById(id);
    }
    @Override
    public List<SupplierManagePageVO> list() {
        return supplierManageDao.listOverride();
    }
    @Override
    public void binding(SupplierManageBindingDTO supplierManageBindingDTO) {
        UserInfo userInfo = userInfoDao.getOneByUserTel(supplierManageBindingDTO.getUserTel());
        if (BeanUtil.isEmpty(userInfo)) {
            throw new GlobalRunTimeException("该手机号非平台用户");
        }

        SupplierManage supplierManage = new SupplierManage();
        supplierManage.setUserId(userInfo.getId());
        supplierManage.setId(supplierManageBindingDTO.getId());
        supplierManage.setUserTel(supplierManageBindingDTO.getUserTel());
        supplierManageDao.updateById(supplierManage);
    }

}
